﻿/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: HUYNH PHI LONG
Created Date	: 12/20/2010
Modified By     :
Modified Date   :
Note            :

*/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;

namespace CommonTagTime.DataAccess
{
    public class daoVideo
    {
        public daoVideo()
        { 
            
        }
        public bool DeActive(int VideoID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busVideo.VIDEOID, SqlDbType.Int);
                para[0].Value = VideoID;
                result = clsSQL.SPExecute("Video_DeActive", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoVideo", "DeActive(int VideoID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Active(int VideoID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busVideo.VIDEOID, SqlDbType.Int);
                para[0].Value = VideoID;
                result = clsSQL.SPExecute("Video_Active", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoVideo", "Active(int VideoID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool OrderDown(int VideoID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busVideo.VIDEOID, SqlDbType.Int);
                para[0].Value = VideoID;
                result = clsSQL.SPExecute("Video_OrderDown", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoVideo", "OrderDown(int VideoID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool OrderUp(int VideoID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busVideo.VIDEOID, SqlDbType.Int);
                para[0].Value = VideoID;
                result = clsSQL.SPExecute("Video_OrderUp", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoVideo", "OrderUp(int VideoID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Delete(int VideoID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busVideo.VIDEOID, SqlDbType.Int);
                para[0].Value = VideoID;
                result = clsSQL.SPExecute("Video_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoVideo", "Delete(Video objVideo)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public int Insert(Video objVideo)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            Video item = new Video();
            try
            {
                //para[0] = new SqlParameter(busVideo.VideoFile, SqlDbType.NVarChar);
                //para[0].Value = clsConvert.StringToDB(objVideo.VideoFile);
                //para[1] = new SqlParameter(busVideo.SortOrder, SqlDbType.Int);
                //para[1].Value = objVideo.SortOrder;
                para[0] = new SqlParameter(busVideo.ISACTIVE, SqlDbType.Bit);
                para[0].Value = objVideo.IsActive;
                //para[3] = new SqlParameter(busVideo.ThumbImage, SqlDbType.NVarChar);
                //para[3].Value = clsConvert.StringToDB(objVideo.ThumbImage);
                para[1] = new SqlParameter(busVideo.NAME, SqlDbType.NVarChar);
                para[1].Value = clsConvert.StringToDB(objVideo.Name);
                para[2] = new SqlParameter(busVideo.VIDEOFILE, SqlDbType.NVarChar);
                para[2].Value = clsConvert.StringToDB(objVideo.VideoFile);
                para[3] = new SqlParameter(busVideo.DESCRIPTION, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objVideo.Description);
                para[4] = new SqlParameter(busVideo.CREATEDBY, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objVideo.CreatedBy);
                para[5] = new SqlParameter(busVideo.CREATEDDATE, SqlDbType.DateTime);
                para[5].Value = objVideo.CreatedDate;
                result = clsSQL.SPExecute("Video_Insert", para);

            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoVideo", "Insert(Video)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public int Update(Video objVideo)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[7];
            try
            {
                para[0] = new SqlParameter(busVideo.VIDEOFILE, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objVideo.VideoFile);
                para[1] = new SqlParameter(busVideo.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objVideo.IsActive;
                para[2] = new SqlParameter(busVideo.NAME, SqlDbType.NVarChar);
                para[2].Value = clsConvert.StringToDB(objVideo.Name);
                para[3] = new SqlParameter(busVideo.DESCRIPTION, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objVideo.Description);
                para[4] = new SqlParameter(busVideo.UPDATEDBY, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objVideo.UpdatedBy);
                para[5] = new SqlParameter(busVideo.UPDATEDDATE, SqlDbType.DateTime);
                para[5].Value = objVideo.UpdatedDate;
                para[6] = new SqlParameter(busVideo.VIDEOID, SqlDbType.Int);
                para[6].Value = objVideo.VideoID;
                result = clsSQL.SPExecute("Video_Update", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoVideo", "Update(Video objVideo)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;

        }
        public Video Detail(Video Video)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Video item = new Video();

            try
            {
                para[0] = new SqlParameter(busVideo.VIDEOID, SqlDbType.Int);
                para[0].Value = Video.VideoID;

                using (de = clsSQL.SPDataReader("Video_Detail", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoVideo", "Detail(Video Video)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }
        private Video Load(IDataReader de)
        {
            Video item = new Video();
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.Description = clsConvert.DbToString("Description", de);
            item.IsActive = clsConvert.DbToBoolean("IsActive", de);
            item.Name = clsConvert.DbToString("Name", de);
            item.VideoID = clsConvert.DbToInt("VideoID", de);
            item.UpdatedBy = clsConvert.DbToString("UpdatedBy", de);
            item.UpdatedDate = clsConvert.DbToDateTime("UpdatedDate", de);
            item.VideoFile = clsConvert.DbToString("VideoFile", de);
            item.SortOrder = clsConvert.DbToInt("SortOrder", de);
            item.ThumbImage = clsConvert.DbToString("ThumbImage", de);
            return item;
        }

        public VideoCollection List(Video objVideo)
        {

            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            VideoCollection items = new VideoCollection();
            try
            {
                para[0] = new SqlParameter(busVideo.CONDITION, SqlDbType.NVarChar);
                para[0].Value = objVideo.Condition;
                para[1] = new SqlParameter(busVideo.ORDERBY, SqlDbType.NVarChar);
                para[1].Value = objVideo.OrderBy;
                para[2] = new SqlParameter(busVideo.ORDERDIRECTION, SqlDbType.NVarChar);
                para[2].Value = objVideo.OrderDirection;
                para[3] = new SqlParameter(busVideo.CURRENTPAGE, SqlDbType.Int);
                para[3].Value = objVideo.CurrentPage;
                para[4] = new SqlParameter(busVideo.PAGESIZE, SqlDbType.Int);
                para[4].Value = objVideo.PageSize;
                para[5] = new SqlParameter(busVideo.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("Video_List", para);
                while (de.Read())
                {
                    Video item = Load(de);
                    items.Add(item);
                }

                de.Close();
                objVideo.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoVideo", "List(Video Video)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }

        public Video GetByID(int VideoID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Video item = new Video();
            try
            {
                para[0] = new SqlParameter(busVideo.VIDEOID, SqlDbType.Int);
                para[0].Value = VideoID;
                using (de = clsSQL.SPDataReader("Video_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoVideo", " GetByID(int VideoID)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }

    }
    
}
